<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.evaluateId ? '新增' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="40%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="星级" prop="starClass">
        <span>
          <el-rate
            v-model="dataForm.starClass"
            :colors="colors" @change="starClassChange">
          </el-rate>
        </span>
      </el-form-item>
      <el-form-item label="标签" prop="content">
        <el-input v-model="dataForm.content" placeholder="标签" style="width: 200px"></el-input>
      </el-form-item>
      <el-form-item label="标签内容" prop="starContent">
        <el-select v-model="dataForm.starId" placeholder="请选择内客">
          <el-option 
            v-for="item in dataForm.contentList"
            :key="item.starId"
            :label="item.commentContent"
            :value="item.starId">
          </el-option>
        </el-select>
        <!-- <el-input v-model="dataForm.content" placeholder="标签" style="width: 200px"></el-input> -->
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input type="number" v-model="dataForm.sort" placeholder="排序" style="width: 200px"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dialogVisible: false,
        dataForm: {
          evaluateId: 0,
          starClass: '',
          content: '',
          sort: '',
          starContent: '',
          starId: '',
          contentList: []
        },
        dataRule: {
          starClass: [
            { required: true, message: '星级不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '标签不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.starClass = ''
        this.dataForm.content = ''
        this.dataForm.sort = ''
        this.dataForm.starId = ''
        this.dataForm.starContent = ''
        this.dataForm.contentList = []
        this.dataForm.evaluateId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.evaluateId) {
            this.$http({
              url: this.$http.adornUrl(`/good/comment/getEvaluateClassDetailsById/${this.dataForm.evaluateId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.starClass = data.data.starClass
                this.dataForm.content = data.data.content
                this.dataForm.sort = data.data.sort
                this.dataForm.starContent = data.data.starContent
                // 请求内客
                this.$http({
                  url: this.$http.adornUrl(`/comment/star/queryStarList/${this.dataForm.starClass}`),
                  method: 'get',
                  params: this.$http.adornParams()
                }).then(({data}) => {
                  debugger
                  if (data && data.code === 0) {
                    this.dataForm.contentList = data.data
                  }
                })
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        // if (this.dataForm.starId === null || this.dataForm.starId === '') {
        //   this.$message.error('标签内客不能为空')
        //   return
        // }
        // console.log(this.dataForm.starId)
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/good/comment/${!this.dataForm.evaluateId ? 'addEvaluateClass' : 'modifyEvaluateClassById'}`),
              method: 'post',
              data: this.$http.adornData({
                'evaluateId': this.dataForm.evaluateId || undefined,
                'starClass': this.dataForm.starClass,
                'sort': this.dataForm.sort,
                'content': this.dataForm.content,
                'starId': this.dataForm.starId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      starClassChange (starId) {
        this.$http({
          url: this.$http.adornUrl(`/comment/star/queryStarList/${starId}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataForm.contentList = data.data
          }
        })
      }
    }
  }
</script>
